package com.example.medicalequipment.ui.activity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;

import androidx.activity.EdgeToEdge;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.medicalequipment.R;
import com.example.medicalequipment.controller.RepairController;
import com.example.medicalequipment.entity.MedicalDevice;
import com.example.medicalequipment.ui.adapter.RepairAdapter;

import java.util.ArrayList;
import java.util.List;

public class RepairApprovalActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private RepairAdapter adapter;
    private List<MedicalDevice> repairList = new ArrayList<>();
    private RepairController repairController;

    // 注册Activity结果回调
    private ActivityResultLauncher<Intent> detailLauncher = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            result -> {
                if (result.getResultCode() == RESULT_OK) {
                    // 刷新列表数据
                    loadRepairRequests();
                }
            }
    );

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_repair_approval);

        // 初始化视图和控制器
        initViews();
        repairController = new RepairController(this, null);

        // 加载维修申请列表
        loadRepairRequests();
    }

    private void initViews() {
        recyclerView = findViewById(R.id.recycler_repair_requests);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new RepairAdapter(repairList, this);
        recyclerView.setAdapter(adapter);

        // 设置点击事件
        adapter.setOnItemClickListener((position, device) -> {
            // 跳转到维修单详情页
            Intent intent = new Intent(this, RepairDetailActivity.class);
            intent.putExtra("deviceId", device.getId());
            detailLauncher.launch(intent);
        });
    }

    private void loadRepairRequests() {
        // 在后台线程中获取维修申请列表
        new AsyncTask<Void, Void, List<MedicalDevice>>() {
            @Override
            protected List<MedicalDevice> doInBackground(Void... voids) {
                return repairController.getAllRepairRequests();
            }

            @Override
            protected void onPostExecute(List<MedicalDevice> devices) {
                if (devices != null) {
                    repairList.clear();
                    repairList.addAll(devices);
                    adapter.notifyDataSetChanged();
                }
            }
        }.execute();
    }
}